package pl.us.ij.plugins.segmentation.forms;

import com.intellij.uiDesigner.core.Spacer;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import pl.us.ij.plugins.segmentation.commands.filters.MedianFilterCommand;
import pl.us.ij.plugins.segmentation.commands.filters.MedianFilterCommandParams;
import pl.us.ij.plugins.segmentation.infrastructure.CategoryInfo;
import pl.us.ij.plugins.segmentation.infrastructure.CommandParams;
import pl.us.ij.plugins.segmentation.localization.Localization;

import javax.swing.*;
import java.util.ResourceBundle;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-05-25
 * Time: 23:48:56
 * To change this template use File | Settings | File Templates.
 */
public class FilterMedianForm extends CategoryInfo {
    private JPanel medianPanel;
    private JSpinner medianIterationCountSpinner;
    private JComboBox medianMaskSizeCombo;
    private JCheckBox medianBlackCheckBox;
    private JCheckBox medianWhiteCheckBox;
    private JButton medianApplyButton;

    public FilterMedianForm(IActionHandler actionHandler) {
        medianIterationCountSpinner.setModel(new SpinnerNumberModel(1, 1, 10, 1));

        actionHandler.setAction(medianApplyButton, MedianFilterCommand.class, getMedianFilterParams());
    }

    @Override
    public String getTitle() {
        return Localization.getString("filterMedianForm.title");
    }

    @Override
    public JPanel getPanel() {
        return medianPanel;
    }

    @Override
    public String getDescription() {
        return null;
    }

    private ICommandParamGather getMedianFilterParams() {
        return new ICommandParamGather() {
            public CommandParams getCommandParams() {
                MedianFilterCommandParams params = new MedianFilterCommandParams();
                params.setIterationCount(((Number) medianIterationCountSpinner.getValue()).intValue());
                params.setMaskSize(Integer.parseInt(String.valueOf(medianMaskSizeCombo.getSelectedItem())));
                params.setWhite(medianWhiteCheckBox.isSelected());
                params.setBlack(medianBlackCheckBox.isSelected());

                return params;
            }
        };
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:p:noGrow", "center:d:noGrow"));
        medianPanel = new JPanel();
        medianPanel.setLayout(new FormLayout("fill:p:noGrow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:d:grow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        CellConstraints cc = new CellConstraints();
        panel1.add(medianPanel, cc.xy(1, 1, CellConstraints.LEFT, CellConstraints.FILL));
        final JLabel label1 = new JLabel();
        this.$$$loadLabelText$$$(label1, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("filterMedianForm.iterationCountLabel"));
        medianPanel.add(label1, cc.xy(1, 1));
        medianIterationCountSpinner = new JSpinner();
        medianPanel.add(medianIterationCountSpinner, cc.xy(1, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
        final com.intellij.uiDesigner.core.Spacer spacer1 = new com.intellij.uiDesigner.core.Spacer();
        medianPanel.add(spacer1, cc.xy(1, 13, CellConstraints.DEFAULT, CellConstraints.FILL));
        final JLabel label2 = new JLabel();
        this.$$$loadLabelText$$$(label2, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("filterMedianForm.maskSizeLabel"));
        medianPanel.add(label2, cc.xy(1, 5));
        medianMaskSizeCombo = new JComboBox();
        final DefaultComboBoxModel defaultComboBoxModel1 = new DefaultComboBoxModel();
        defaultComboBoxModel1.addElement("3");
        defaultComboBoxModel1.addElement("5");
        defaultComboBoxModel1.addElement("7");
        defaultComboBoxModel1.addElement("9");
        defaultComboBoxModel1.addElement("11");
        defaultComboBoxModel1.addElement("13");
        defaultComboBoxModel1.addElement("15");
        defaultComboBoxModel1.addElement("17");
        defaultComboBoxModel1.addElement("19");
        defaultComboBoxModel1.addElement("21");
        medianMaskSizeCombo.setModel(defaultComboBoxModel1);
        medianPanel.add(medianMaskSizeCombo, cc.xy(1, 7));
        medianBlackCheckBox = new JCheckBox();
        this.$$$loadButtonText$$$(medianBlackCheckBox, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("filterMedianForm.blackLabel"));
        medianPanel.add(medianBlackCheckBox, cc.xy(1, 9));
        medianWhiteCheckBox = new JCheckBox();
        this.$$$loadButtonText$$$(medianWhiteCheckBox, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("filterMedianForm.whiteLabel"));
        medianPanel.add(medianWhiteCheckBox, cc.xy(1, 11));
        medianApplyButton = new JButton();
        medianApplyButton.setHideActionText(false);
        this.$$$loadButtonText$$$(medianApplyButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("filterMedianForm.filterLabel"));
        medianApplyButton.setVerticalAlignment(0);
        medianPanel.add(medianApplyButton, cc.xy(1, 15, CellConstraints.FILL, CellConstraints.BOTTOM));
        label1.setLabelFor(medianIterationCountSpinner);
        label2.setLabelFor(medianMaskSizeCombo);
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadButtonText$$$(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }
}
